﻿using System;
using System.Collections.Generic;
using System.Text;
using Young3.FMSearch.Business.Managers;
using Young3.FMSearch.Business.Offsets;

namespace Young3.FMSearch.Business.Entities.InGame
{
    public class Club : BaseObject
    {
        public Club(int memoryAddress)
            : base(memoryAddress)
        { }

        public Staff Chairman
        {
            get { return new Staff(ChairmanPointer); }
        }

        public string Name
        {
            get { return ProcessManager.ReadString(MemoryAddress + ClubOffsets.Name, null); }
        }

        public string ShortName
        {
            get { return ProcessManager.ReadString(MemoryAddress + ClubOffsets.ShortName, null); }
        }

        public string NickName
        {
            get { return ProcessManager.ReadString(MemoryAddress + ClubOffsets.NickName, null); }
        }

        public string SixLetterName
        {
            get { return ProcessManager.ReadString(MemoryAddress + ClubOffsets.SixLetterName, null); }
        }

        public string SecondNickName
        {
            get { return ProcessManager.ReadString(MemoryAddress + ClubOffsets.SecondNickName, null); }
        }

        public int Morale
        {
            get { return ProcessManager.ReadInt16(MemoryAddress + ClubOffsets.Morale); }
            set { ProcessManager.WriteInt16(value, MemoryAddress + ClubOffsets.Morale); }
        }

        public int YearFounded
        {
            get { return ProcessManager.ReadInt16(MemoryAddress + ClubOffsets.YearFounded); }
            set { ProcessManager.WriteInt16(value, MemoryAddress + ClubOffsets.YearFounded); }
        }

        public byte NumberOfAffiliatedClubs
        {
            get
            {
                return ProcessManager.ReadByte(MemoryAddress + ClubOffsets.NumberOfAffiliatedClubs);
            }
            set
            {
                ProcessManager.WriteByte(value, MemoryAddress + ClubOffsets.NumberOfAffiliatedClubs);
            }
        }

        public byte MaxAffiliatedClubNumber
        {
            get
            {
                return ProcessManager.ReadByte(MemoryAddress + ClubOffsets.MaxAffiliatedClubNumber);
            }
            set
            {
                ProcessManager.WriteByte(value, MemoryAddress + ClubOffsets.MaxAffiliatedClubNumber);
            }
        }

        public int ID
        {
            get { return ProcessManager.ReadInt32(MemoryAddress + ClubOffsets.ID); }
        }

        public int SeasonTicketHolders
        {
            get
            {
                return ProcessManager.ReadInt32(MemoryAddress + ClubOffsets.SeasonTicketHolders);
            }
            set
            {
                ProcessManager.WriteInt32(value, MemoryAddress + ClubOffsets.SeasonTicketHolders);
            }
        }

        public int AverageAttendance
        {
            get
            {
                return ProcessManager.ReadInt32(MemoryAddress + ClubOffsets.AverageAttendance);
            }
            set
            {
                ProcessManager.WriteInt32(value, MemoryAddress + ClubOffsets.AverageAttendance);
            }
        }

        public byte TeamNumber
        {
            get { return ProcessManager.ReadByte(MemoryAddress + ClubOffsets.TeamNumber); }
            set { ProcessManager.WriteByte(value, MemoryAddress + ClubOffsets.TeamNumber); }
        }

        public int ClubsNumber
        {
            get { return ProcessManager.ReadInt16(MemoryAddress + ClubOffsets.ClubsNumber); }
            set { ProcessManager.WriteInt16(value, MemoryAddress + ClubOffsets.ClubsNumber); }
        }

        public int DirectorNumber
        {
            get { return ProcessManager.ReadInt16(MemoryAddress + ClubOffsets.DirectorNumber); }
            set { ProcessManager.WriteInt16(value, MemoryAddress + ClubOffsets.DirectorNumber); }
        }

        public int ScoutNumber
        {
            get { return ProcessManager.ReadInt16(MemoryAddress + ClubOffsets.ScoutNumber); }
            set { ProcessManager.WriteInt16(value, MemoryAddress + ClubOffsets.ScoutNumber); }
        }

        private int ScoutAddress
        {
            get { return ProcessManager.ReadInt32(MemoryAddress + ClubOffsets.ScoutAddress); }
            set { ProcessManager.WriteInt32(value, MemoryAddress + ClubOffsets.ScoutAddress); }
        }
        
        private int FinanceAddress
        {
            get { return ProcessManager.ReadInt32(MemoryAddress + ClubOffsets.FinanceAddress); }
            set { ProcessManager.WriteInt32(value, MemoryAddress + ClubOffsets.FinanceAddress); }
        }

        private int CountryAddress
        {
            get { return ProcessManager.ReadInt32(MemoryAddress + ClubOffsets.Country); }
            set { ProcessManager.WriteInt32(value, MemoryAddress + ClubOffsets.Country); }
        }

        public Country Country
        {
            get { return new Country(CountryAddress); }
            set { CountryAddress = value.MemoryAddress; }
        }

        public Finance Finances
        {
            get
            {
                return new Finance(FinanceAddress);
            }
        }

        public int MaximumAttendance
        {
            get
            {
                return ProcessManager.ReadInt32(MemoryAddress + ClubOffsets.MaximumAttendance);
            }
            set
            {
                ProcessManager.WriteInt32(value, MemoryAddress + ClubOffsets.MaximumAttendance);
            }
        }

        public int MinimumAttendance
        {
            get
            {
                return ProcessManager.ReadInt32(MemoryAddress + ClubOffsets.MinimumAttendance);
            }
            set
            {
                ProcessManager.WriteInt32(value, MemoryAddress + ClubOffsets.MinimumAttendance);
            }
        }

        public ClubStatus Status
        {
            get
            {
                return (ClubStatus)ProcessManager.ReadByte(MemoryAddress + ClubOffsets.Status);
            }
            set
            {
                ProcessManager.WriteByte((byte)value, MemoryAddress + ClubOffsets.Status);
            }
        }

        public byte TrainingGround
        {
            get
            {
                return ProcessManager.ReadByte(MemoryAddress + ClubOffsets.TrainingGround);
            }
            set
            {
                ProcessManager.WriteByte(value, MemoryAddress + ClubOffsets.TrainingGround);
            }
        }

        public byte YouthGround
        {
            get { return ProcessManager.ReadByte(MemoryAddress + ClubOffsets.YouthGround); }
            set { ProcessManager.WriteByte(value, MemoryAddress + ClubOffsets.YouthGround); }
        }

        public byte YouthAcademy
        {
            get { return ProcessManager.ReadByte(MemoryAddress + ClubOffsets.YouthAcademy); }
            set { ProcessManager.WriteByte(value, MemoryAddress + ClubOffsets.YouthAcademy); }
        }

        public int TeamAddress
        {
            get { return ProcessManager.ReadInt32(MemoryAddress + ClubOffsets.TeamAddress); }
            set { ProcessManager.WriteInt32(value, MemoryAddress + ClubOffsets.TeamAddress); }
        }

        public int DirectorAddress
        {
            get { return ProcessManager.ReadInt32(MemoryAddress + ClubOffsets.DirectorAddress); }
            set { ProcessManager.WriteInt32(value, MemoryAddress + ClubOffsets.DirectorAddress); }
        }

        private int ChairmanPointer
        {
            get { return ProcessManager.ReadInt32(MemoryAddress + ClubOffsets.Chairman); }
        }

        public List<Team> Teams
        {
            get
            {
                List<Team> ret = new List<Team>();
                for (int i = 0; i < TeamNumber; i++)
                    ret.Add(new Team(ProcessManager.ReadInt32(TeamAddress + (i * 0x4))));
                return ret;
            }
        }

        public List<Staff> Scouts
        {
            get
            {
                List<Staff> ret = new List<Staff>();
                for (int i = 0; i < ScoutNumber; i++)
                    ret.Add(new Staff(ProcessManager.ReadInt32(ScoutAddress + (i * 0x4))));
                return ret;
            }
        }

        public List<Staff> Directors
        {
            get
            {
                List<Staff> ret = new List<Staff>();
                for (int i = 0; i < DirectorNumber; i++)
                    ret.Add(new Staff(ProcessManager.ReadInt32(DirectorAddress + (i * 0x4))));
                return ret;
            }
        }

        //TODO: Implement Kit

        //TODO: Implement Sponsor
    }

    public enum ClubStatus
    {
        Professional = 1,
        SemiProfessional = 2,
        Amateur = 3
    }
}